/**====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*

lcd_dev.c

Copyright (c) 2003 by CHIPNUTS Incorporated. All Rights Reserved.
*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*/

/**===========================================================================

 Revisions of key_dev.h
 Version  		Name       	Date			Description
 1.0  		Tim			12/06/2005  	Initial Version  

*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*/
 
#include "lcd_ILI9320_16BIT.h"
#include "comdef.h"
#include "sys.h"

#define chp_delay ustc_delay

void lcd_init(void)
{
	dl_lcd_ILI9320_back_light_on(20);

	dl_lcd_init_ILI9320_16BIT();
	dl_lcd_ILI9320_16BIT_disp_block(0,0,240,320,0xf800);
}

//USE GPIOC11
DL_DISP_RET_T dl_lcd_ILI9320_back_light_on(UINT8 cnt)
{
	UINT8 i;

	SYS_OUTMASK_UINT32(IO_PCONC,0xc00000,0x400000);		
	
	for(i = cnt; i>0; i--)
	{ 
		SYS_OUTMASK_UINT32( IO_PDATC,0x0800,0x0800);
		SYS_OUTMASK_UINT32( IO_PDATC,0x0800,0x0000);
	}
	
	SYS_OUTMASK_UINT32( IO_PDATC,0x0800,0x0800);
	
//	sys_delay(20);
	
	return DL_DISP_RET_SUCCESS; 
}


DL_DISP_RET_T dl_lcd_ILI9320_reset(void)
{
	//USE SYSRST
	return DL_DISP_RET_SUCCESS;
}


UINT16 dl_lcd_read_ILI9320_16BIT(unsigned char addr)
{
	UINT32	data;
	
	OUT_UINT16(LCD_ILI9320_16BIT_ADDR, addr);
	data = IN_UINT16(LCD_ILI9320_16BIT_DATA); 
	
	return data;
}


void dl_lcd_write_ILI9320_16BIT(unsigned int addr,unsigned int data)
{
	LCD_ADDR_WRITE_ILI9320_16BIT(addr);
	LCD_DATA_WRITE_ILI9320_16BIT(data);	
}


DL_DISP_RET_T dl_lcd_init_ILI9320_16BIT(void)  
{

	//************* Start Initial Sequence **********//                                      
	dl_lcd_write_ILI9320_16BIT(0x00E5, 0x8000); // Set the Vcore voltage and this setting is must.
	dl_lcd_write_ILI9320_16BIT(0x0000, 0x0001); // Start internal OSC.                            
	dl_lcd_write_ILI9320_16BIT(0x0001, 0x0100); // set SS and SM bit                              
	dl_lcd_write_ILI9320_16BIT(0x0002, 0x0700); // set 1 line inversion                           
	dl_lcd_write_ILI9320_16BIT(0x0003, 0x1030); // set GRAM write direction and BGR=0.            
	dl_lcd_write_ILI9320_16BIT(0x0004, 0x0000); // Resize register                                
	dl_lcd_write_ILI9320_16BIT(0x0008, 0x0202); // set the back porch and front porch             
	dl_lcd_write_ILI9320_16BIT(0x0009, 0x0000); // set non-display area refresh cycle ISC[3:0]    
	dl_lcd_write_ILI9320_16BIT(0x000A, 0x0000); // FMARK function  
	dl_lcd_write_ILI9320_16BIT(0x000C, 0x0000); // RGB interface setting                          
	dl_lcd_write_ILI9320_16BIT(0x000D, 0x0000); // Frame marker Position   
	dl_lcd_write_ILI9320_16BIT(0x000F, 0x0000); // RGB interface polarity                         
	//*************Power On sequence ****************//                                      
	dl_lcd_write_ILI9320_16BIT(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB               
	//dl_lcd_write_ILI9320_16BIT(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]        
	dl_lcd_write_ILI9320_16BIT(0x0011, 0x0007); 
	dl_lcd_write_ILI9320_16BIT(0x0012, 0x0000); // VREG1OUT voltage                         
	dl_lcd_write_ILI9320_16BIT(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude
	//sys_delay(200); // Dis-charge capacitor power voltage                                      
	chp_delay(200); ////added by Gonifer Liu, CUNTS Shanghai, 2007-06-20  
	dl_lcd_write_ILI9320_16BIT(0x0010, 0x17B0); // SAP, BT[3:0], AP, DSTB, SLP, STB               
	//dl_lcd_write_ILI9320_16BIT(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]               
	dl_lcd_write_ILI9320_16BIT(0x0011, 0x0007);
	//sys_delay(50); // Delay 50ms       
	chp_delay(50); ////added by Gonifer Liu, CUNTS Shanghai, 2007-06-20  
	dl_lcd_write_ILI9320_16BIT(0x0012, 0x013E); // VREG1OUT voltage                               
	//sys_delay(50); // Delay 50ms    
	chp_delay(50); ////added by Gonifer Liu, CUNTS Shanghai, 2007-06-20  
	dl_lcd_write_ILI9320_16BIT(0x0013, 0x1300); // VDV[4:0] for VCOM amplitude                    
	dl_lcd_write_ILI9320_16BIT(0x0029, 0x0006); // VCM[4:0] for VCOMH                             
	//sys_delay(50);       
	chp_delay(50); ////added by Gonifer Liu, CUNTS Shanghai, 2007-06-20  
	dl_lcd_write_ILI9320_16BIT(0x0020, 0x0000); // GRAM horizontal Address                        
	dl_lcd_write_ILI9320_16BIT(0x0021, 0x0000); // GRAM Vertical Address                          
	// ----------- Adjust the Gamma Curve ----------//                                       
	dl_lcd_write_ILI9320_16BIT(0x0030, 0x0000);                                                   
	dl_lcd_write_ILI9320_16BIT(0x0031, 0x0505);                                                   
	dl_lcd_write_ILI9320_16BIT(0x0032, 0x0004);                                                   
	dl_lcd_write_ILI9320_16BIT(0x0035, 0x0006);                                                   
	dl_lcd_write_ILI9320_16BIT(0x0036, 0x0707);                                                   
	dl_lcd_write_ILI9320_16BIT(0x0037, 0x0105);                                                   
	dl_lcd_write_ILI9320_16BIT(0x0038, 0x0002);                                                   
	dl_lcd_write_ILI9320_16BIT(0x0039, 0x0707);                                                   
	dl_lcd_write_ILI9320_16BIT(0x003C, 0x0704);                                                   
	dl_lcd_write_ILI9320_16BIT(0x003D, 0x0807);                                                   
	//------------------ Set GRAM area ---------------//                                     
	dl_lcd_write_ILI9320_16BIT(0x0050, 0x0000); // Horizontal GRAM Start Address  
	dl_lcd_write_ILI9320_16BIT(0x0051, 0x00EF); // Horizontal GRAM End Address                
	dl_lcd_write_ILI9320_16BIT(0x0052, 0x0000); // Vertical GRAM Start Address
	dl_lcd_write_ILI9320_16BIT(0x0053, 0x013F); // Vertical GRAM End Address
	dl_lcd_write_ILI9320_16BIT(0x0060, 0x2700); // Gate Scan Line             
	dl_lcd_write_ILI9320_16BIT(0x0061, 0x0001); // NDL,VLE, REV               
	dl_lcd_write_ILI9320_16BIT(0x006A, 0x0000); // set scrolling line         
	//-------------- Partial Display Control ---------//                 
	dl_lcd_write_ILI9320_16BIT(0x0080, 0x0000);                               
	dl_lcd_write_ILI9320_16BIT(0x0081, 0x0000);                               
	dl_lcd_write_ILI9320_16BIT(0x0082, 0x0000);                               
	dl_lcd_write_ILI9320_16BIT(0x0083, 0x0000);                               
	dl_lcd_write_ILI9320_16BIT(0x0084, 0x0000);                               
	dl_lcd_write_ILI9320_16BIT(0x0085, 0x0000);                               
	//-------------- Panel Control -------------------//                 
	dl_lcd_write_ILI9320_16BIT(0x0090, 0x0010);                               
	dl_lcd_write_ILI9320_16BIT(0x0092, 0x0000);                               
	dl_lcd_write_ILI9320_16BIT(0x0093, 0x0003);                               
	dl_lcd_write_ILI9320_16BIT(0x0095, 0x0110);                               
	dl_lcd_write_ILI9320_16BIT(0x0097, 0x0000);                               
	dl_lcd_write_ILI9320_16BIT(0x0098, 0x0000);                               
	dl_lcd_write_ILI9320_16BIT(0x0007, 0x0173); // 262K color and display ON  

	chp_delay(200); ////added by Gonifer Liu, CUNTS Shanghai, 2007-06-20  
	
	return DL_DISP_RET_SUCCESS;
}


DL_DISP_RET_T dl_lcd_ILI9320_16BIT_set_disp_win(UINT16 angle, UINT16 x_start ,UINT16 y_start ,UINT16 width ,UINT16 height)
{
	UINT16 x_end,y_end;
	UINT16 gram_start_x;
	UINT16 gram_start_y;

	dl_lcd_ILI9320_16BIT_set_scanmode(angle);

	if (angle&1)
	{//switch height and width
		width^=height;
		height=width^height;
		width^=height;
	}
	
	x_end = x_start+width-1;
	y_end = y_start+height-1;

	gram_start_x=x_start;
	gram_start_y=y_start;
	
	switch (angle)
	{
		case 1:
			gram_start_x=x_end;
			break;
		case 2:
			gram_start_x=x_end;
			gram_start_y=y_end;
			break;
		case 3:
			gram_start_y=y_end;
	}
	
	/*set the win*/
	dl_lcd_write_ILI9320_16BIT(0x50,x_start);
	dl_lcd_write_ILI9320_16BIT(0x51,x_end);
	
	dl_lcd_write_ILI9320_16BIT(0x52,y_start);
	dl_lcd_write_ILI9320_16BIT(0x53,y_end);

	/*set GRAM start address */	
	dl_lcd_write_ILI9320_16BIT(0x20, gram_start_x);
	dl_lcd_write_ILI9320_16BIT(0x21, gram_start_y);
	
	/*set GRAM write command */
	LCD_ADDR_WRITE_ILI9320_16BIT(0x22);

	return DL_DISP_RET_SUCCESS;
}


DL_DISP_RET_T dl_lcd_ILI9320_16BIT_disp_block(UINT16 x_start ,UINT16 y_start ,UINT16 width ,UINT16 height,UINT16 color)
{
	UINT32 i,j;

	dl_lcd_ILI9320_16BIT_set_disp_win(0, x_start, y_start, width, height);	

	for (j=0; j<width; j++)
	{
		LCD_DATA_WRITE_ILI9320_16BIT(0x1F);
	}

	for(i=1; i<height; i++)
	{
		LCD_DATA_WRITE_ILI9320_16BIT(0x1F);
		for (j=1; j<width; j++)
		{
			LCD_DATA_WRITE_ILI9320_16BIT(color);
		}
	}

	return DL_DISP_RET_SUCCESS;
}

DL_DISP_RET_T dl_lcd_ILI9320_16BIT_display_scn(UINT16 x_start ,UINT16 y_start ,UINT16 width ,UINT16 height, UINT8 *p_buf)
{
	UINT32 i;
	UINT16 *p;

	dl_lcd_ILI9320_16BIT_set_disp_win(0, x_start, y_start, width, height);	

	p = (UINT16 *)p_buf;

	for(i=0; i<height*width; i++)
	{
		LCD_DATA_WRITE_ILI9320_16BIT	(*(p++));
	}

	return DL_DISP_RET_SUCCESS;
}


DL_DISP_RET_T dl_lcd_ILI9320_16BIT_set_scanmode(UINT8 scanmode)
{
	
	switch(scanmode)
	{
		case 0:
			dl_lcd_write_ILI9320_16BIT(0x0003, 0x1030); 
			break;

		case 1:
			dl_lcd_write_ILI9320_16BIT(0x0003, 0x1028); 
			break;

		case 2:
			dl_lcd_write_ILI9320_16BIT(0x0003, 0x1000); 
			break;

		case 3:
			dl_lcd_write_ILI9320_16BIT(0x0003, 0x1018); 
			break;
			
		default:
			break;
	}
	
	return DL_DISP_RET_SUCCESS;
}


